package com.stardata.liangshisq.tcslmon.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stardata.liangshisq.tcslmon.entity.BizBs;
import com.stardata.liangshisq.tcslmon.entity.KcKsc;
import com.stardata.liangshisq.tcslmon.entity.TbBizBsTime;
import com.stardata.liangshisq.tcslmon.entity.TbKcKscTime;
import com.stardata.liangshisq.tcslmon.service.*;
import com.stardata.liangshisq.tcslmon.util.BeanUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 将 Order信息和Ksc信息同步到 tb_biz_bs和 tb_bkc_ksc 表中
 * 将个人业务表和客户业务表反剋实现数据的同步
 */
@Component
@AllArgsConstructor
public class OrderSyncTask {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final TbBizBsTimeService tbBizBsTimeService;

    private final TbKcKscTimeService tbKcKscTimeService;


    private final BizBsService bizBsService ;

    private final KcKscService kcKscService ;

    private final OrderInfoService orderInfoService;

    /**
     * 同步订单信息到表
     * 1分钟一次
     */
    @Scheduled(cron = "* 0/1 * * * ?")
    public void OrderSync() {
        //1.查询当日的order信息
        QueryWrapper<TbBizBsTime> wrapper = new QueryWrapper<TbBizBsTime>();
        LocalDateTime now = LocalDateTime.now();
        wrapper.between("open_time", now.with(LocalTime.MIN), now.with(LocalTime.MAX));
        List<TbBizBsTime> tbBizBsTimeList = tbBizBsTimeService.list(wrapper);
        List<Long> bsIds = new ArrayList<>();
        for(TbBizBsTime item:tbBizBsTimeList){
            bsIds.add(item.getId());
        }
        // 2. 查询 没有同步的Id信息
        logger.debug("一分钟执行一次，查询没有同步到 tb_biz_bs_time 表中的数据 ");
        QueryWrapper<BizBs> bizWrapper = new QueryWrapper<BizBs>();
        bizWrapper.between("open_time", now.with(LocalTime.MIN), now.with(LocalTime.MAX));
        if(!bsIds.isEmpty()){
            bizWrapper.notIn("id", bsIds);
        }
        List<BizBs> bizBsList = bizBsService.list(bizWrapper);
        for(BizBs item: bizBsList){
            TbBizBsTime tbBizBsTime = new TbBizBsTime();
            BeanUtils.copyProperties(item,tbBizBsTime);
            tbBizBsTimeService.saveOrUpdate(tbBizBsTime);
        }
    }

    /**
     * 同步订单信息到表
     * 1分钟一次
     */
    @Scheduled(cron = "* 0/1 * * * ?")
    public void OrderKscSync() {
        //1.查询当日的order信息
        QueryWrapper<TbKcKscTime> wrapper = new QueryWrapper<TbKcKscTime>();
        LocalDateTime now = LocalDateTime.now();
        wrapper.between("call_up_time", now.with(LocalTime.MIN), now.with(LocalTime.MAX));
        List<TbKcKscTime> tbKcKscTimeList = tbKcKscTimeService.list(wrapper);
        List<Long> bsIds = new ArrayList<>();
        for(TbKcKscTime item:tbKcKscTimeList){
            bsIds.add(item.getId());
        }
        // 2. 查询 没有同步的Id信息
        logger.debug("一分钟执行一次，查询没有同步到 tb_kc_ksc 表中的数据 ");
        QueryWrapper<KcKsc> kscWrapper = new QueryWrapper<KcKsc>();
        if(!bsIds.isEmpty()){
            kscWrapper.notIn("id", bsIds);
        }
        kscWrapper.between("call_up_time", now.with(LocalTime.MIN), now.with(LocalTime.MAX));
        List<KcKsc> kcKscList = kcKscService.list(kscWrapper);
        for(KcKsc item: kcKscList){
            TbKcKscTime tbKcKscTime = new TbKcKscTime();
            BeanUtils.copyProperties(item,tbKcKscTime);
            tbKcKscTimeService.saveOrUpdate(tbKcKscTime);
        }
    }

}
